package com.yuec.design_pattern;

import java.util.Scanner;
//工厂模式
public class Factory {
    public static void main(String[] args) {
        new OrderPizza(new SimpleFactory());
    }
}

//订购pizza
class OrderPizza{
    SimpleFactory simpleFactory;
    Pizza pizza;

    public OrderPizza (SimpleFactory simpleFactory){
        setFactory(simpleFactory);
    }

    public void setFactory(SimpleFactory simpleFactory){
        String orderType="";//用户输入
        this.simpleFactory = simpleFactory;
        do {
            orderType=getType();
            pizza=this.simpleFactory.createPizza(orderType);
            if (pizza!=null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("订购失败");
            }

        }while(true);
    }

    public String getType(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要订购的pizza：");
        return scanner.nextLine();
    }
}



//简单工厂
class SimpleFactory {
    Pizza pizza;
    public Pizza createPizza(String orderType) {
        if (orderType.equals("cheese")) {
            pizza = new CheesePizza();
            pizza.setName("奶酪披萨");
        }else if(orderType.equals("pepper")){
            pizza = new PepperPizza();
            pizza.setName("胡椒披萨");
        }
        return pizza;
    }
}

//芝士披萨
class CheesePizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println();
    }
}

//芝士披萨
class PepperPizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println();
    }
}

//pizza
abstract class Pizza {
    protected String name;

    //准备原材料不同的pizza，抽象方法
    public abstract void prepare();

    //烘烤
    public void bake() {
        System.out.println(name + "baking");
    }

    //切割
    public void cut() {
        System.out.println(name + "cuting");
    }

    //打包
    public void box() {
        System.out.println(name + "boxing");
    }

    public void setName(String name) {
        this.name = name;
    }
}
